//
// Created by Zhangwj on 2023/9/25.
//

#include "CompUnitNode.h"


std::unique_ptr<CompUnitNode> CompUnitNode::CompUnitParser(Parser &parser) {
    //CompUnit → {Decl} {FuncDef} MainFuncDef
    auto compUnitNode = std::make_unique<CompUnitNode>();
    while (parser.getLexType(0) == LexType::VOIDTK ||
           (parser.getLexType(0) == LexType::INTTK && parser.getLexType(2) != LexType::LPARENT)) {
        compUnitNode->Decls.push_back(DeclNode::DeclParser(parser));
    }
    while (parser.getLexType(2) == LexType::LPARENT) {
        compUnitNode->FuncDefs.push_back(FuncDefNode::FuncDefParser(parser));
    }
    if (parser.getLexType(1) == LexType::MAINTK) {
        compUnitNode->MainFuncDef = MainFuncDefNode::MainFuncDefParser(parser);
    }
    return compUnitNode;
}
